package com.chezhujie.www.controller;



import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.chezhujie.www.core.page.Pagination;
import com.chezhujie.www.domain.Czjawarduse;
import com.chezhujie.www.domain.Czjroulette;
import com.chezhujie.www.domain.vo.CzjawarduseVO1;
import com.chezhujie.www.enums.OrderStatus;
import com.chezhujie.www.service.CzjawarduseService;
import com.chezhujie.www.service.CzjrouletteService;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;



@Controller
@RequestMapping("awarduse")
public class CzjawarduseController /*extends BaseController*/{
	private static final Logger log = LoggerFactory.getLogger(CzjawarduseController.class);
	public CzjawarduseController() {
	}
 
 
 	/**
	 * czj_awarduser 奖品用户新增页面
	 * 
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("czjawarduseAdd.do")
	public String add(HttpServletRequest request, ModelMap model) {
		model.addAttribute("czjawarduse", new Czjawarduse());
      setDict(model);
		return "awarduser/czjawarduseEdit";
	}

	/**
	 * czj_awarduser 奖品用户保存
	 * 
	 * @param czjawarduse
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("czjawarduseSave.do")
	public String save(Czjawarduse czjawarduse, HttpServletRequest request, ModelMap model) throws Exception{
    
		try {
			if(czjawarduse.getAwarduseId()!=null&&!czjawarduse.getAwarduseId().equals("")){
				// 编辑
				czjawarduseService.updateCzjawarduse(czjawarduse);
				//tlogService.insertTloger(LoggerHelper.getTloger(request, LoggerHelper.OPERATING, "编辑czj_awarduser 奖品用户", "awarduseId="+czjawarduse.getAwarduseId()));
			} else {
				// 新增
				czjawarduse.setCreatetime(new Date());
				czjawarduse.setStatus(OrderStatus.UNRECEIVED.getIndex());
				czjawarduseService.insertCzjawarduse(czjawarduse);
				//tlogService.insertTloger(LoggerHelper.getTloger(request, LoggerHelper.OPERATING, "新增czj_awarduser 奖品用户", "awarduseId="+czjawarduse.getAwarduseId()));
			}
   		} catch (Exception e) {
   			log.error("czjawarduse save error:", e); 
            throw e;
   		}
      
		return "redirect:czjawarduseList.do";
	}

	/**
	 * czj_awarduser 奖品用户删除
	 * 
	 * @param ids
	 * @return
	 */
	@RequestMapping("czjawarduseDelete.do")
	public String delete(@RequestParam("ids") List<String> awarduseIdList) throws Exception{
		try {
         if (awarduseIdList != null) {
   			czjawarduseService.deleteCzjawarduse(awarduseIdList);
   		}
         } catch (Exception e) {
   			log.error("czjawarduse delete error:", e); 
            throw e;
   		}
		return "redirect:czjawarduseList.do";
	}
	 
	/**
	 * czj_awarduser 奖品用户编辑页面
	 * 
	 * @param awarduseId
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("czjawarduseEdit.do")
	public String edit(String awarduseId, HttpServletRequest request, ModelMap model) throws Exception{
      try {
   		Map<String, Object> param = new HashMap<String, Object>();
   		param.put("awarduseId", awarduseId);
         CzjawarduseVO1 czjawarduse = czjawarduseService.getCzjawarduseVO1(param);
   		model.addAttribute("czjawarduse", czjawarduse);
         model.addAttribute("actionEdit", request.getParameter("actionEdit"));
         setDict(model);
         } catch (Exception e) {
   			log.error("czjawarduse edit error:", e); 
            throw e;
   		}
		return "awarduser/czjawarduseEdit";
	}

	/**
	 * czj_awarduser 奖品用户详情
	 * 
	 * @param awarduseId
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("czjawarduseDetail.do")
	public String detail(String awarduseId, HttpServletRequest request, HttpServletResponse response, ModelMap model) throws Exception{
      try {
      		Map<String, Object> param = new HashMap<String, Object>();
      		param.put("awarduseId", awarduseId);
            Czjawarduse czjawarduse = czjawarduseService.getCzjawarduse(param);
      		model.addAttribute("czjawarduse", czjawarduse);
            setDict(model);
            } catch (Exception e) {
   			log.error("czjawarduse detail error:", e); 
            throw e;
   		  }
		return "awarduser/czjawarduseView";
	}
	 
/**
 * 
 * @author 
 * @date 2017年7月17日 上午11:15:29
 * @param czjawarduse
 * @param pageNo
 * @param order
 * @param nickname
 * @param telephone
 * @param thingName
 * @param rouletteId
 * @param startTime
 * @param endTime
 * @param serviceName
 * @param countTel
 * @param status
 * @param sort
 * @param request
 * @param model
 * @return
 * @throws Exception
 */
	@RequestMapping("czjawarduseList.do")
	public String listPage(Czjawarduse czjawarduse, Integer pageNo, String order,			
			String nickname,String telephone,String thingName,
			String rouletteId,String startTime,String endTime,String serviceName,
			String countTel,String status,String countName,
			String sort,HttpServletRequest request, ModelMap model) throws Exception{
      try {   
   		if (pageNo == null)
   			pageNo = 1;
   		 Map<String, Object> param = new HashMap<String, Object>();
   		 param.put("nickname", nickname);
   		 param.put("telephone", telephone);
   		 param.put("thingName", thingName);
   		 param.put("rouletteId", rouletteId);
   		 if(startTime!=null && !"".equals(startTime)){
   			 param.put("startTime", startTime);
   		 }
   		 if(endTime!=null && !"".equals(endTime)){
   			 param.put("endTime", endTime);
   		 }
   		 param.put("serviceName", serviceName);
   		 param.put("countTel", countTel);
   		 param.put("status", status);
   		 param.put("countName", countName);
         Pagination pagination = czjawarduseService.getCzjawarduseListName(param, 20, pageNo,
   				order, sort);
 		request.setAttribute("nickname",nickname);
   		request.setAttribute("telephone",telephone);
   		request.setAttribute("thingName",thingName);
   		request.setAttribute("rouletteId",rouletteId);
   		request.setAttribute("startTime",startTime);
   		request.setAttribute("endTime",endTime);
   		request.setAttribute("serviceName",serviceName);
   		request.setAttribute("countTel",countTel);
   		request.setAttribute("status",status);
   		request.setAttribute("countName",countName); 		
   		//把所有活动查出来
   		Map<String, Object> param123 = new HashMap<String, Object>();
   		List<Czjroulette> czjroulettes = czjrouletteService.getCzjrouletteListALL1(param123); 		
   		model.addAttribute("czjroulettes", czjroulettes);
   		model.addAttribute("pagination", pagination);
         setDict(model);
         } catch (Exception e) {
			log.error("czjawarduse listPage error:", e); 
         throw e;
		   }
		return "awarduser/czjawarduseList";
	}
	 
	/**
	 * czj_awarduser 奖品用户查询
	 * 
	 * @param czjawarduse
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("czjawarduseList1.do")
	public String list(Czjawarduse czjawarduse, HttpServletRequest request, ModelMap model) throws Exception{
      try {
   		Map<String,Object> map = czjawarduseService.getCzjawarduseList(toMap(czjawarduse));
   		model.addAttribute("list", map.get("rows"));
   		model.addAttribute("total", map.get("total"));
         setDict(model);
         } catch (Exception e) {
   			log.error("czjawarduse list error:", e); 
            throw e;
   		}
		return "awarduser/czjawarduseList";
	}
	 

 	/***
	 * 将页面输入信息转成map
	 * 
	 * @param czjawarduse
	 * @return
	 */	private Map<String,Object> toMap(Czjawarduse czjawarduse){
		Map<String,Object> map=new HashMap<String, Object>();
      		if(czjawarduse.getAwarduseId()!=null&&!czjawarduse.getAwarduseId().equals("")){
			map.put("awarduseId", czjawarduse.getAwarduseId());
		}
		if(czjawarduse.getUserInfoId()!=null&&!czjawarduse.getUserInfoId().equals("")){
			map.put("userInfoId", czjawarduse.getUserInfoId());
		}
		if(czjawarduse.getAwardId()!=null&&!czjawarduse.getAwardId().equals("")){
			map.put("awardId", czjawarduse.getAwardId());
		}
		if(czjawarduse.getCount()!=null&&!czjawarduse.getCount().equals("")){
			map.put("count", czjawarduse.getCount());
		}
		if(czjawarduse.getStatus()!=null&&!czjawarduse.getStatus().equals("")){
			map.put("status", czjawarduse.getStatus());
		}
		if(czjawarduse.getAwardType()!=null&&!czjawarduse.getAwardType().equals("")){
			map.put("awardType", czjawarduse.getAwardType());
		}
		if(czjawarduse.getRouletteId()!=null&&!czjawarduse.getRouletteId().equals("")){
			map.put("rouletteId", czjawarduse.getRouletteId());
		}
		if(czjawarduse.getCreatetime()!=null&&!czjawarduse.getCreatetime().equals("")){
			map.put("createtime", czjawarduse.getCreatetime());
		}
		if(czjawarduse.getExt1()!=null&&!czjawarduse.getExt1().equals("")){
			map.put("ext1", czjawarduse.getExt1());
		}
		if(czjawarduse.getExt2()!=null&&!czjawarduse.getExt2().equals("")){
			map.put("ext2", czjawarduse.getExt2());
		}
		if(czjawarduse.getExt3()!=null&&!czjawarduse.getExt3().equals("")){
			map.put("ext3", czjawarduse.getExt3());
		}      
		return map;
	}
   
	/**
	 * 设置页面所需的数据字典
	 * @param model
	 */
	private void setDict(ModelMap model){
		
	}



   
      
	@Resource
	private CzjawarduseService czjawarduseService;
	
	@Resource
	private CzjrouletteService czjrouletteService;


}